package com.chang.demo.utils;

import com.chang.demo.bean.FileDtoBean;

import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class FileUtil {
    /**
     * 通过文件夹路径获取此文件夹下所有文件
     * @param path
     * @return
     */
    public static ArrayList<FileDtoBean> getFilesByDictionary (String path) {
        File file = new File(path);
        if (file != null && file.isDirectory()) {
            return FileUtil.getChildFiles(file, "");
        }
        ArrayList<FileDtoBean> newFileList = new ArrayList<>();
        if (file != null) {
            FileDtoBean bean = new FileDtoBean();
            bean.setRelativePath(path);
            bean.setFile(file);
            newFileList.add(bean);
        }
        return newFileList;
    }

    /**
     * 获取文件夹下的所有文件
     * @param file
     * @return
     */
    public static ArrayList<FileDtoBean> getChildFiles (File file, String relativePath) {
        File[] files = file.listFiles();
        ArrayList<FileDtoBean> newFileList = new ArrayList<>();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                ArrayList childFileList = FileUtil.getChildFiles(file1, relativePath + "." + file1.getName());
                newFileList.addAll(childFileList);
            } else {
                FileDtoBean bean = new FileDtoBean();
                bean.setRelativePath(relativePath);
                bean.setFile(file1);
                newFileList.add(bean);
            }
        }
        return newFileList;
    }
}
